package edu.uoc.tds.pac4.client.ss4;

import edu.uoc.tds.pac4.client.PanelClientInterface;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.rmi.RemoteException;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import edu.uoc.tds.pac4.beans.Participacio2;
import edu.uoc.tds.pac4.beans.Participant;
import edu.uoc.tds.pac4.beans.Usuari;
import edu.uoc.tds.pac4.client.Client;
import edu.uoc.tds.pac4.common.GestioEventsException;
import edu.uoc.tds.pac4.common.PantallaError;
import edu.uoc.tds.pac4.common.TDSLanguageUtils;
import edu.uoc.tds.pac4.interfaces.RemoteInterface;

public class PantallaConsultaAssistencia extends PantallaInscripcions implements PanelClientInterface  {

		private static final long serialVersionUID = 1L;
		private JPanel jPanelContainer = null;
		private jPanelLlistatAssistencia jPanelParticipacio = null;
		private JPanelFiltreConsultaAssistencia jPanelFiltre = null;
		private Vector<Participacio2> currentParticipacio = null;
		private Client client= null;
		private JLabel jLabelUsuari = null;
		private JLabel jLabelFieldUsuari = null;
		private Usuari usuari = null;
		private Participant participant = null;
		private JPanel jPanelTitol = null;
		private JLabel jLabelTitolPantalla = null;
		
		/**
		 * Constructor
		 */
		public PantallaConsultaAssistencia() {
			super();
			initialize();
		}

		/**
		 * Constructor que rep una referencia al servidor remot i client
		 * @param client  
		 * @param remot
		 */
		public PantallaConsultaAssistencia(Client client, RemoteInterface remot) {
			// TODO Auto-generated constructor stub
			this.remot = remot;
			this.client = client;
			initialize();
		}

		/**
		 * Inicialitza la pantalla
		 */
		protected void initialize() {
			usuari = client.getUserConnected();				
		}

		/**
		 * This method initializes jPanelContainer
		 * 
		 * @return javax.swing.JPanel
		 */
		@Override
		public JPanel getJPanelContainer(Dimension dimension) {
			if (jPanelContainer == null) {
				
				// Titol
				GridBagConstraints gbcPanelTitol = new GridBagConstraints();
				gbcPanelTitol.gridx = 0;
				gbcPanelTitol.gridy = 0;
				gbcPanelTitol.gridwidth = 3;
				gbcPanelTitol.gridheight = 3;
				gbcPanelTitol.fill = GridBagConstraints.HORIZONTAL;
				gbcPanelTitol.anchor = GridBagConstraints.PAGE_START;
				gbcPanelTitol.insets = new Insets(10, 10, 10, 10);
								
				//Filtre
				GridBagConstraints gbcPanelFiltre = new GridBagConstraints();
				gbcPanelFiltre.gridx = 0;
				gbcPanelFiltre.gridy = 4;
				
				//Llistat Participacio
				GridBagConstraints gbcPanelParticipacio = new GridBagConstraints();
				gbcPanelParticipacio.gridx = 0;
				gbcPanelParticipacio.gridy = 5;
				gbcPanelParticipacio.weightx = 1;
				gbcPanelParticipacio.weighty = 1;
				gbcPanelParticipacio.insets = new Insets(0, 10, 10, 10);
				gbcPanelParticipacio.fill = GridBagConstraints.BOTH;		
															
				jPanelContainer = new JPanel();
				jPanelContainer.setSize(dimension);
				jPanelContainer.setLayout(new GridBagLayout());
				jPanelContainer.add(getJPanelTitol(), gbcPanelTitol);				
				jPanelContainer.add(getJPanelFiltre(), gbcPanelFiltre);
				jPanelContainer.add(getJPanelParticipacio(), gbcPanelParticipacio);					
			}
			return jPanelContainer;
		}
		/**
		 * This method initializes jPanelTitol
		 * 
		 * @return javax.swing.JPanel
		 */
		private JPanel getJPanelTitol() {
			if (jPanelTitol == null) {
				Insets titleInset = new Insets(10, 15, 10, 10);
				Insets textInset = new Insets(10, 5, 10, 25);
				
				// Usuari Participant
				GridBagConstraints gbcLabelUsuari = new GridBagConstraints();
				gbcLabelUsuari.gridx = 2;
				gbcLabelUsuari.gridy = 0;
				gbcLabelUsuari.insets = titleInset;
				jLabelUsuari = new JLabel();		
				jLabelUsuari.setText(TDSLanguageUtils.getMessage("Client.PantallaConsultaEvents.Usuari"));
				GridBagConstraints gbcTextUsuari = new GridBagConstraints();
				gbcTextUsuari.gridx = 3;
				gbcTextUsuari.gridy = 0;
				gbcTextUsuari.insets = textInset;
				jLabelFieldUsuari = new JLabel();
				jLabelFieldUsuari.setText(usuari.getNom()+" "+usuari.getCognoms()+ "     " );
		
				jLabelTitolPantalla = new JLabel(TDSLanguageUtils.getMessage("Client.PantallaConsultaAssistencia.Titol"));
				GridBagConstraints gbcTitolUsuari = new GridBagConstraints();
				gbcTitolUsuari.gridx = 1;
				gbcTitolUsuari.gridy = 0;
				gbcTitolUsuari.insets = titleInset;

				jPanelTitol = new JPanel();
				jPanelTitol.setLayout(new GridBagLayout());
				jPanelTitol.setBorder(BorderFactory.createLineBorder(Color.GRAY));
				jPanelTitol.add(jLabelTitolPantalla, gbcTitolUsuari);
				jPanelTitol.add(jLabelUsuari, gbcLabelUsuari);
				jPanelTitol.add(jLabelFieldUsuari, gbcTextUsuari);
			}
			return jPanelTitol;
		}
		

		/**
		 * This method initializes jPanelFiltre
		 * 
		 * @return javax.swing.JPanel
		 */
		private JPanel getJPanelFiltre() {
			if (jPanelFiltre == null) {
				jPanelFiltre = new JPanelFiltreConsultaAssistencia(this);
			}
			return jPanelFiltre;
		}

		/**
		 * This method initializes jPanelParticipacio
		 * 
		 * @return JPanelParticipacio
		 */
		private jPanelLlistatAssistencia getJPanelParticipacio() {
			if (jPanelParticipacio == null) {
				jPanelParticipacio = new jPanelLlistatAssistencia();
			}
			return jPanelParticipacio;
		}
				
		/**
		 * Actualitza el contingut de la taula
		 */
		public void updateTaula() {	
			String userCodi = usuari.getCodi();
				// crida al servidor per rebre els Participacio2
				currentParticipacio = null;	
				participant = null;
				try {		
					participant = remot.getParticipant(userCodi);
					currentParticipacio = remot.getParticipacions2(participant);
				} catch (RemoteException e) {
					new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
				} catch (GestioEventsException e) {
					new PantallaError(e.getMessage(), PantallaError.Tipus.ERROR);
				}			
				jPanelParticipacio.drawParticipacio(currentParticipacio);
		}
}

			
	